package edu.xhu;

import java.util.ArrayList;

import com.bean.ExamineBean;
import com.constant.Constant;
import com.data.ExamineData;
import com.net.ClientError;
import com.net.LoadData;
import com.net.RequestListener;
import com.net.RequestParameter;

import edu.xhu.adapters.EduSystemExameAdapter;
import edu.xhu.view.LoadingDialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 考试信息查询模块
 * @author swerit
 *
 */

public class EduSystemExame extends Activity{

	private Button mBtnBack;					//返回按钮
//	private TextView mTitle;					//标题
	private ListView mExameListView;
	private XhuApplication application;
	private ArrayList<ExamineBean> mExamineList;
	private EduSystemExameAdapter adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edusystem_exame);
		
		initView();
		setListener();
		loadExame();
	}
	
	private void initView(){
		mExameListView			= (ListView) findViewById(R.id.listview_exame);
		mBtnBack				= (Button) findViewById(R.id.button_back);
//		mTitle					= (TextView) findViewById(R.id.title);
		application				= (XhuApplication) getApplication();
		
//		mTitle.setText("欢迎您：" + application.getUserName());
	}
	
	private void setListener(){
		mBtnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private void loadExame(){
		mBtnBack.post(new Runnable() {
			public void run() {
				LoadingDialog.showWindow(EduSystemExame.this);
			}
		});
		
		RequestParameter parameter = new RequestParameter();
		parameter.add("url", application.getExamineUrl());
		parameter.add("referer", application.getRefererUrl());
		parameter.add("name", application.getUserName().replaceAll("同学", ""));
		
		LoadData.loadData(Constant.EXAMINE_DATA, parameter, new RequestListener() {
			public void onError(ClientError ed) {
				LoadingDialog.dismiss();
				ErrorShow.showError(EduSystemExame.this, ed);
			}
			
			public void onComplete(Object obj) {
				ExamineData data = (ExamineData) obj;
				data.printData();
				
				mExamineList = data.getExamineList();
				if (null == mExamineList){
					LoadingDialog.dismiss();
					Toast.makeText(EduSystemExame.this, "没有考试信息", Toast.LENGTH_SHORT).show();
					return;
				}
				
				adapter = new EduSystemExameAdapter(EduSystemExame.this, mExamineList);
				mExameListView.setAdapter(adapter);
				
				LoadingDialog.dismiss();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK){
			if (LoadingDialog.isShowing())
				LoadingDialog.dismiss();
			else {
				finish();
			}
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
